package org.sorbonne.lex.service;

import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;

import org.sorbonne.lex.dao.LexDAO;
import org.sorbonne.lex.model.LemmeMorph;
import org.sorbonne.lex.model.LexemeMorph;

public class LexServiceImpl implements LexService {
	private LexDAO lexDAO;

	public void setLexDAO(LexDAO lexDAO) {
		this.lexDAO = lexDAO;
	}

	public Set<LemmeMorph> findLemmeMorphSet(String lexeme) {
		return lexDAO.findLemmeMorphSet(lexeme.trim().toLowerCase());
	}

	public Set<String> findLexemes(String tag, Collection<String> props) {
		Set<String> result = new TreeSet<String>();
		Set<LexemeMorph> lexemeMorphSet = lexDAO.findLexemeMorphSetByTag(tag);
		if (props == null || props.isEmpty()) {
			return findLexemes(tag);
		}
		boolean found;
		for (LexemeMorph lexemeMorph : lexemeMorphSet) {
			found = true;
			for (String p : props) {
				if (!lexemeMorph.getMorph().contains(p)) {
					found = false;
					break;
				}
			}
			if (found)
				result.add(lexemeMorph.getLexeme());
		}
		return result;
	}

	private Set<String> findLexemes(String tag) {
		Set<String> result = new TreeSet<String>();
		Set<LexemeMorph> lexemeMorphSet = lexDAO.findLexemeMorphSetByTag(tag);
		if (lexemeMorphSet == null)
			return result;
		for (LexemeMorph lexemeMorph : lexemeMorphSet) {
			result.add(lexemeMorph.getLexeme());
		}
		return result;
	}

}
